<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
    <h1>this is index page</h1>
    <button @click="toInsert()" >前往增加页面</button>
    <table border="1">
        <tr>
            <th>userID</th>
            <th>姓名</th>
            <th>密码</th>
            <th>email</th>
            <th>tel</th>
            <th>操作</th>
        </tr>
        <tr v-for="(item,index) in userList">
            <td>{{item.userId}}</td>
            <td>{{item.userName}}</td>
            <td>{{item.password}}</td>
            <td>{{item.email}}</td>
            <td>{{item.tel}}</td>
            <td>
                <button @click="deleteOne(item.userId)">删除</button>
                <button @click="update(item.userId)">修改</button>
            </td>
        </tr>
    </table>
</div>



<script>

    new Vue({
        el: '#app',
        data: {
            userList: []
        },
        methods: {
            getAllUsers: function () {
                //使用axios请求后端获取数据
                _this = this;
                axios.get('/user/findAll').then(function (res) {
                    _this.userList = res.data;
                })
            },
            update: function (href) {
                window.localStorage.setItem("userId",href);
                window.location='update.html';
            },
            deleteOne: function (href) {
                axios.post('/user/delete/'+href).then(function (res) {
                    if ( res!=null ) {
                        window.location="index.html";
                    } else {
                        alert("你的操作有误");
                    }
                })
            },
            toInsert: function () {
                window.location="insert.html";
            }
        },
        mounted: function(){

        },
        created: function () {
            this.getAllUsers();
        }
    })

</script>

</body>
</html>